class panorama{
        constructor () {
            this.scene = new THREE.Scene();
            this.initCamera();
            this.initRenderer();
            this.initModal();
            this.initControl();
            this.animate();
			this.state=false;
        }

        initCamera () {
            let camera = this.camera = new THREE.PerspectiveCamera( 100, window.innerWidth / window.innerHeight, 1, 1100 );
            camera.position.set(0, 0, 1);
        }

        initModal () {
            
            var sphereGeometry = new THREE.SphereGeometry(500, 60, 40);
            
            sphereGeometry.scale(1, 1, -1);
            const texture = new THREE.TextureLoader().load("img/room_low/ccc.jpg")
            var sphereMaterial = new THREE.MeshBasicMaterial({
            	map: texture
            });
            
            
            var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
      
            this.scene.add(sphere);
        }
        initRenderer () {
            let container = document.getElementById( 'container' );
            let renderer = this.renderer = new THREE.WebGLRenderer({ logarithmicDepthBuffer: true });
            renderer.setPixelRatio( window.devicePixelRatio );
            renderer.setSize( window.innerWidth, window.innerHeight );
            renderer.sortObjects = false;
            renderer.autoClear = false;
            container.appendChild( renderer.domElement );
        }
        initControl(){
          if(this.controls){
            this.controls.dispose()
          }
          this.controls = new THREE.DeviceOrientationControls( this.camera );
          this.controls.connect();
        }
        animate() {
            this.render();
            requestAnimationFrame( ()=>{this.animate()});
        }
	
	

		 controlsState(){
			if(this.state){
				//更新控制器
				this.controls.update();
			
			}else{
			this.controls.enabled = false;
			//更新控制器
			this.controls.update();
			}
		
		}
        render() {
            TWEEN.update();
			this.controlsState()
            this.renderer.render(this.scene, this.camera);
        }
    }
	
	
	window.onload = function(){
		document.getElementById("tlyBtn").addEventListener('click', ()=>{
			pano.state = !pano.state;
			console.log(pano.state)
		}) 
	}
    let pano = new panorama();
	console.log(pano.state)
    document.addEventListener('click', (event) => {
      if(window.DeviceOrientationEvent && window.DeviceOrientationEvent.requestPermission){
        window.DeviceOrientationEvent.requestPermission().then(state => {
          switch (state) {
            case "granted":
              pano.initControl()
              break;
            case "denied":
              alert("你拒绝了使用陀螺仪");
              break;
            case "prompt":
              alert("其他行为");
              break;
          }
        }).catch((e)=>{
          alert(e);
        });
      }
    })